How can I use the SQL aggregate function to obtain the number of affected rows for addition, deletion, and modification? It is usually very useful to calculate the number of rows returned or affected by a query before performing further operations.
This article mainly introduces how php uses the exec () function under PDO to query the number of affected rows after execution, and analyzes the exec () When php uses pdo for addition, deletion, and modification () implementation tips and
1. @ Error
If an error occurs in the current statement, the error code is returned. Otherwise, 0 is returned. Note that @ error is reset immediately after each statement is executed. Therefore, check the value after the statement to be verified is
In normal mode, $ pdo-& amp; gt; exec ($ SQL); can be used to return the number of affected rows. How can I know the number of affected rows if data is inserted to the pre-processed object? $ Stmt $ pdo-& amp; gt; prepare ($ SQL); $ stmt-& amp; gt;
Essence: Read operation, with mysql_num_rows function, write operation with mysql_affected_rows functionmysql_num_rows () returns the number of rows in the result set. This command is valid only for SELECT statements. To get the number of rows
In SQL Server, the number of affected rows returned is: If @ @RowCount In Oracle, the number of affected rows returned is: If sql%rowcountCases:Sql server:Create procedureproc_test@Stat int=0, @MsgId varchar( -)="', as BEGIN
After the update operation is completed in the stored procedure, @ rowcount is used to return the number of affected rows. If multiple statements exist, you can define the number of rows affected by variable storage. Example:
Alter procedure [DBO].
Oracle stored procedures with input and output parameters Return Affected rows SQL % ROWCOUNT [SQL] CREATE OR REPLACE PROCEDURE PROC_UPDATE_TEMP (P_DAYS IN NUMBER, -- execution days P_ROWS OUT VARCHAR2, -- returns the NUMBER of affected rows
In sqlserver, the number of affected rows returned is: If @ rowcount
In Oracle, the number of affected rows returned is: If SQL % rowcount
Example:
Sqlserver:
Asbeginupdate t_mt set stat = @ stat, ostat = @ stat, rpttime = getdate () where
Check for changes with the HasChanges method.
First use the HasChanges method to detect the affected rows (return bool)
if (!mydataset.haschanges (datarowstate.modified)) return;
To create a temporary dataset
DataSet Xdataset;
Using the GetChanges
The database has an order table with a row of data order_id1555 (auto-incrementing primary key) and a tag field order_mark2 to execute the statement. use the mysql client tool QueryOK, 0 rowsaffected (0.00sec) Rowsmatched: 1 Changed: 0 Warnings: 0
One of the power of T-SQL is the large volume of operation data. However, in some scenarios, the number of rows affected by T-SQL needs to be limited. For example, in the past, Yilong encountered a scenario where too many rows were changed at a time
Test the creation of the DB2 stored procedure and use get diagnostics to return the affected rows:
Create a file in. IO in the current directory
--In. IoCreate ProcedureProc1 (Out rowsInteger) Language SQLBeginUpdateTb1SetName=Name; get
Thinkphp I executed an update statement to obtain the affected number of rows. example: PHPcode $ bookM (Book); $ dataarray (); $ data [shengCunQi] time (); $ book-& gt; save ($ data, array (where & gt; & quot; shengcunqi0000-00-00 thinkphp I
I encountered a problem that I rarely encountered today. The record is as follows:
In the database, there is a stored procedure sp_createorupdatexxx that executes the insert or update (already exists) operation,
InProgramWhen SQL command.
Pre-Preparation:CREATE TABLE Nums (X int);CREATE TABLE T (X int);GoPurpose: Insert the table Nums | Delete | The update is reflected in the T table.------------------------------------------------------------------------------------------------------
For example: I now want to execute so many statements at the same time update community set XQMC=replace (XQMC,"',‘‘);Update Community Set XQBM=replace (XQBM,"',‘‘);Update Community Set DKXX=replace (DKXX,"',‘‘);Update Community Set ssqy=replace
I learned from manual that the mysql_affected_rows function will return abnormal values when the data before and after UPDATE is the same,
The following is a convenient solution. The bdobrica at gmail dot com message is displayed on the official
In mysql, row_count () is used to directly make such a judgment. This statement must be followed by the SQL statement you run. node. js's I/o operations are asynchronous, which leads to a problem. It is not good to judge which SQL statement
The mysql_affected_rows function returns an abnormal value when the data before and after UPDATE is the same, next, I will introduce you to a friend who is interested in simple solutions. refer to the following. I learned from manual that the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.